<?php

namespace app\index\model;

class FunctionResult
{
    // 返回状态
    public $status;
    // 返回消息
    public $message;
    // 返回数据
    public $data;

    public function __construct($message, $data = [], $status = false)
    {
        if(!is_string($message)){
            $message = (string) $message;
        }
        $this->message = $message;

        if($status != true){
            $status = false;
        }
        $this->status = $status;

        if(!is_array($data)){
            $data = [];
        }
        $this->data = $data;
    }

    /**
     * 操作成功
     * @Author   zhanghong(Laifuzi)
     * @DateTime 2019-02-16
     * @param    string             $message 提示信息
     * @param    array              $data    返回结果
     * @return   Object                      FunctionResult
     */
    public function success($message, $data = [])
    {
        $this->status = true;

        if(is_string($message)){
            $this->message = $message;
        }

        if(is_array($data)){
            $this->data = $data;
        }

        return $this;
    }

    /**
     * 操作失败
     * @Author   zhanghong(Laifuzi)
     * @DateTime 2019-02-16
     * @param    string             $message 提示信息
     * @param    array              $data    返回结果
     * @return   Object                      FunctionResult
     */
    public function fail($message, $data = [])
    {
        $this->status = false;

        if(is_string($message)){
            $this->message = $message;
        }

        if(is_array($data)){
            $this->data = $data;
        }

        return $this;
    }

    /**
     * 给返回数据添加单个值
     * @Author   zhanghong(Laifuzi)
     * @DateTime 2019-02-16
     * @param    string             $name  参数名
     * @param    any                $value 参数值
     * @return   Object                    FunctionResult
     */
    public function pushData($name, $value)
    {
        $this->data[$name] = $value;
        return $this;
    }

    /**
     * 给返回数据添加多个值
     * @Author   zhanghong(Laifuzi)
     * @DateTime 2019-02-16
     * @param    array              $array 返回值数组
     * @return   Object                    FunctionResult
     */
    public function mergeData($array)
    {
        if(is_array($array)){
            $this->data = array_merge($this->data, $array);
        }
        return $this;
    }
}